// Functions associated with the Menu class
//-----------------

//-----------------
// Includes libraries and other files
#include "Menu.h"
//-----------------

//-----------------
//// CONSTRUCTOR - Menu
Menu::Menu()
	: p_ui_( nullptr), number_of_buttons_( 0)
{}

//// CONSTRUCTOR - Menu
Menu::Menu( UserInterface* p_ui)
	: p_ui_( p_ui), number_of_buttons_( 0)
{}

//// DESTRUCTOR - ~Menu
Menu::~Menu() {
	p_ui_	= nullptr;
}
//-----------------

//-----------------
//// FUNCTION - get_text_length
// return the length of the button's text
int Menu::get_text_length( const int& i) const {
	// Pre-conditions :- button number must be valid
	assert( i >= 0 && i < number_of_buttons_);

	return( options_[i].get_text_length());
}

//// FUNCTION - get_number_of_buttons
// return the number of buttons that this menu instance contains
int Menu::get_number_of_buttons() const {
	// Pre-conditions :- none
	assert( true);

	return number_of_buttons_;
}

//// FUNCTION - mouseover
// test if the mouse is currently over a button
bool Menu::mouseover( Cursor& mouse, const int& h) const {
	// Pre-conditions :- none
	assert( true);

	// run through the array to find if a button lies under the cursor
	for( int i( 0); i < number_of_buttons_; ++i) {
		if( options_[i].mouseover( mouse.get_x(), mouse.get_y(), h)) {
			mouse.set_active( true);
			return true;
		}
	}

	// if no button is under the cursor then return false
	mouse.set_active( false);
	return false;
}

//// FUNCTION - get_button
// retrieve the number of the button that the mouse is currently over
int Menu::get_button( const Cursor& mouse, const int& h) const {
	// Pre-conditions :- none
	assert( true);

	// run through the array to find the button under the cursor
	for( int i( 0); i < number_of_buttons_; ++i)
		if( options_[i].mouseover( mouse.get_x(), mouse.get_y(), h))
			return i;

	// if no button is under the mouse then return -1
	MessageBox( NULL, "Error Code: 004", "Menu::get_button", MB_OK);
	return -1;
}

//// FUNCTION - render
// output the current menu's buttons onto the screen
void Menu::render() const {
	// Preconditions :- none
	assert( true);

	for( int i( 0); i < number_of_buttons_; ++i)
		render( i);
}

//// FUNCTION - render
// output a specified button from the current menu onto the screen
void Menu::render( const int& i) const {
	// Preconditions :- button number must be valid and the pointer must exist
	assert( i >= 0 && i < number_of_buttons_ && p_ui_ != nullptr);

	p_ui_->render_text( options_[i].get_text(),
						options_[i].get_x_pos(),
						options_[i].get_y_pos(),
						options_[i].get_colour_alpha(),
						options_[i].get_colour_red(),
						options_[i].get_colour_green(),
						options_[i].get_colour_blue());
}

//// FUNCTION - render
// output a specified button from the current menu onto the screen with a specified amount of text
// this function allows for gradual uncovering of buttons
void Menu::render( const int& i, int& text_length) const {
	// Pre-conditions :- button number must be valid and the pointer must exist
	assert( i >= 0 && i < number_of_buttons_ && p_ui_ != nullptr);

	// no assert for text length has been set so that if the value is invalid (either too
	// high or too low) then the length will default to the maximum text length
	if( text_length < 0)
		text_length = options_[i].get_text_length();

	p_ui_->render_text( options_[i].get_text().substr( 0, text_length),
						options_[i].get_x_pos(),
						options_[i].get_y_pos(),
						options_[i].get_colour_alpha(),
						options_[i].get_colour_red(),
						options_[i].get_colour_green(),
						options_[i].get_colour_blue());

	text_length = -1;
}

//// FUNCTION - set_information
// initialise the UserInterface pointer
void Menu::set_information( UserInterface* p_ui) {
	// Pre-conditions :- the pointer must be valid
	assert( p_ui != nullptr);

	p_ui_ = p_ui;
}

//// FUNCTION - create_button
// add a button to the menu's array
void Menu::create_button( const std::string& t, const int& x, const int& y, const int& a, const int& r, const int& g, const int& b) {
	// Pre-conditions :- none
	assert( true);

	options_[number_of_buttons_].set_button( t, x, y, a, r, g, b);
	++number_of_buttons_;
}

//// FUNCTION - reset_buttons
// 'clear' all the previously saved buttons; this doesn't actually delete the information, but makes it inaccessible for further use and unretrievable
void Menu::reset_buttons() {
	// Pre-conditions :- none
	assert( true);

	number_of_buttons_ = 0;
}